package registry

import (
	"kubeInstall/model/registry/http"
)

type ProjectQuery struct {
}

type ProjectReq struct {
}

type CveWhitelist struct {
	CreationTime string `json:"creation_time"`
	ID           int64  `json:"id"`
	Items        []Cve  `json:"items"`
	ProjectID    int64  `json:"project_id"`
	UpdateTime   string `json:"update_time"`
}
type Metadata struct {
	AutoScan             string `json:"auto_scan"`
	EnableContentTrust   string `json:"enable_content_trust"`
	PreventVul           string `json:"prevent_vul"`
	Public               string `json:"public"`
	ReuseSysCveWhitelist string `json:"reuse_sys_cve_whitelist"`
	Severity             string `json:"severity"`
}

type Cve struct {
	CveId string `json:"cve_id"`
}
type Project struct {
	ChartCount         int64        `json:"chart_count"`
	CreationTime       string       `json:"creation_time"`
	CurrentUserRoleID  int64        `json:"current_user_role_id"`
	CurrentUserRoleIds []int64      `json:"current_user_role_ids"`
	CveWhitelist       CveWhitelist `json:"cve_whitelist"`
	Deleted            bool         `json:"deleted"`
	Metadata           Metadata     `json:"metadata"`
	Name               string       `json:"name"`
	OwnerID            int64        `json:"owner_id"`
	OwnerName          string       `json:"owner_name"`
	ProjectID          int64        `json:"project_id"`
	RepoCount          int64        `json:"repo_count"`
	UpdateTime         string       `json:"update_time"`
}

type ListProjectOpts struct {
	Page     int `q:"page"`
	PageSize int `q:"page_size"`

	Name   string `q:"name"`
	Public bool   `q:"public"`

	Owner string `q:"owner"`
}

type ListOptsBuilder interface {
	ToListQuery() (string, error)
}

func (opts ListProjectOpts) ToListQuery() (string, error) {
	q, err := http.BuildQueryString(opts)
	return q.String(), err
}

func ListProject(opts ListOptsBuilder) ([]Project, error) {

	var p []Project
	url := "/api/v2.0/projects"
	if opts != nil {
		query, err := opts.ToListQuery()
		if err != nil {
			return nil, err
		}
		url += query
	}

	resp, err := registryClient.Get(url)

	if err != nil {
		return nil, err
	}

	err = http.ExtractInto(resp.Body, &p)

	if err != nil {
		return nil, err
	}
	return p, nil
}

type CreateProjectOpts struct {
	ProjectName string `json:"project_name"`

	Metadata Metadata `json:"metadata"`

	StorageLimit int `json:"storage_limit"`
}

func CreateProject(opts CreateProjectOpts) error {

	url := "/api/v2.0/projects"

	_, err := registryClient.Post(url, opts)

	if err != nil {
		return err
	}

	return nil

}
